package com.qqclient.view;

import com.qqclient.serivce.FileClientService;
import com.qqclient.serivce.MessageClientService;
import com.qqclient.serivce.UserClientService;
import com.qqclient.utils.Utility;

import java.io.FileOutputStream;

public class ClientView {
    private boolean loop = true;//控制是否显示菜单
    private String key = "";//接收用户键盘的输入
    private UserClientService userClientService = new UserClientService();//用户服务端通信
    private MessageClientService messageClientService = new MessageClientService();//用户之间通信
    private FileClientService fileClientService = new FileClientService();//发送文件

    //启动界面
    public static void main(String[] args) {
        new ClientView().mainMenu();
    }

    //显示主菜单
    private void mainMenu(){
        while (loop){
            System.out.println("==========欢迎登陆网络通信系统========");
            System.out.println("\t\t 1 登陆系统");
            System.out.println("\t\t 9 退出系统");
            System.out.print("请输入你的选择：");

            key = Utility.readString(1);

            //根据用户输入的不同，来处理不同的逻辑
            switch (key){
                case "1":
                    System.out.print("请输入用户号：");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密  码：");
                    String pwd = Utility.readString(50);
                    //做一个验证

                    if (userClientService.checkUser(userId,pwd)){
                        System.out.println("======欢迎(用户"+userId+")登录=====");
                        //进入二级菜单
                        while (loop) {
                            System.out.println("\n======网络通信系统二级菜单（用户"+userId+")========");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.print("请输入你的选择：");
                            key = Utility.readString(1);
                            switch (key){
                                case "1":
                                    //拉取用户在线列表
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.print("请输入群发消息的内容：");
                                    String contents = Utility.readString(100);
                                    //
                                    messageClientService.sendMessageToAll(contents,userId);
                                    break;
                                case "3":
//                                    userClientService.chat();
                                    System.out.print("请输入想聊天的用户(在线)：");
                                    String getterId = Utility.readString(50);
                                    System.out.print("输入消息内容：");
                                    String content = Utility.readString(100);
                                    //编写一个方法，将消息发送给服务器
                                    messageClientService.sendMessageToOne(content,userId,getterId);
                                    break;
                                case "4":
                                    System.out.print("请输入要发送文件的对象：");
                                    getterId = Utility.readString(50);
                                    System.out.print("请输入发送文件的路径：");
                                    String src = Utility.readString(100);
                                    System.out.print("请输入文件发往的路径：");
                                    String dest = Utility.readString(100);
                                    fileClientService.sendfileToOne(src,dest,userId,getterId);
                                    break;
                                case "9":
                                    userClientService.exit();
                                    loop = false;
                                    System.out.println(userId+"用户退出");
                                    break;
                            }
                        }
                    }
                    else {
                        System.out.println("=====登陆失败=====");
                    }
                    break;
                case "":
                    loop = false;
                    break;

            }
        }
    }
}
